#include "config.h"

#include "ms2lstat_mod.h"

#define EMPTY(tok)	(*tok == '-' && tok[1] == '\0')

static ECMBOOL
ms2wgen_parse(char *line, lent_t *lent)
{
	char	*tok, *p;
	char	datetime_str[1024];
	ecm_uint32	n;
	ecm_uint32	status;
	ecm_uint64	file_length;
	ecm_uint64	file_size;

	if (*line == '#')
		return ECM_FALSE;

	/* date */
	if ((tok = ecm_strtok(line, " ", &p)) == NULL) {
		lp_error("cannot find date field");
		return ECM_FALSE;
	}
	n = ecm_snprintf(datetime_str, 1024, "%s ", tok);

	/* time */
	if ((tok = ecm_strtok(NULL, " ", &p)) == NULL) {
		lp_error("cannot find time field");
		return ECM_FALSE;
	}

	ecm_snprintf(datetime_str + n, 1024 - n, "%s", tok);

	lent->tick = lp_get_timetick(datetime_str);
	if (lent->tick == 0) {
		lp_error("invalid date time");
		return ECM_FALSE;
	}

	/* media string */
	if ((tok = ecm_strtok(NULL, " ", &p)) == NULL) {
		lp_error("cannot find media string");
		return ECM_FALSE;
	}

	lent->mid = lp_get_mid(tok);

	/* starttime */
	if ((tok = ecm_strtok(NULL, " ", &p)) == NULL) {
		lp_error("cannot find starttime field");
		return ECM_FALSE;
	}
	if (EMPTY(tok)) {
		lent->startstamp = 0;
	}
	else if (ecm_sscanf(tok, "%u", &lent->startstamp) != 1) {
		lp_error("invalid starttime");
		return ECM_FALSE;
	}

	/* duration */
	if ((tok = ecm_strtok(NULL, " ", &p)) == NULL) {
		lp_error("cannot find duration field");
		return ECM_FALSE;
	}

	if (ecm_sscanf(tok, "%u", &lent->duration) != 1) {
		lp_error("%s: duration format error", tok);
		return ECM_FALSE;
	}

	/* rate */
	if ((tok = ecm_strtok(NULL, " ", &p)) == NULL) {
		lp_error("cannot find rate field");
		return ECM_FALSE;
	}
	if (ecm_sscanf(tok, "%d", &lent->playback_rate) != 1) {
		lp_error("%s: invalid rate value", tok);
		return ECM_FALSE;
	}

	/* status */
	if ((tok = ecm_strtok(NULL, " ", &p)) == NULL) {
		lp_error("cannot find status field");
		return ECM_FALSE;
	}
	if (ecm_sscanf(tok, "%u", &status) != 1) {
		lp_error("%s: invalid status value", tok);
		return ECM_FALSE;
	}
	if (status < 100 || status >= 600) {
		lp_error("%u: invalid status value", status);
		return ECM_FALSE;
	}
	lent->status = status;

	/* playerid */
	if ((tok = ecm_strtok(NULL, " ", &p)) == NULL) {
		lp_error("cannot find playerid field");
		return ECM_FALSE;
	}

	lent->cid = lp_get_cid(tok);

	/* filelength */
	if ((tok = ecm_strtok(NULL, " ", &p)) == NULL) {
		lp_error("cannot find filelength field");
		return ECM_FALSE;
	}
	if (EMPTY(tok)) {
		file_length = 0;
	}
	else if (ecm_sscanf(tok, "%I64u", &file_length) != 1) {
		lp_error("%s: invalid file length", tok);
		return ECM_FALSE;
	}

	/* filesize */
	if ((tok = ecm_strtok(NULL, " ", &p)) == NULL) {
		lp_error("cannot find filesize field");
		return ECM_FALSE;
	}
	if (EMPTY(tok)) {
		file_size = 0;
	}
	else if (ecm_sscanf(tok, "%I64u", &file_size) != 1) {
		lp_error("%s: invalid file size", tok);
		return ECM_FALSE;
	}

	if (status == 200 && file_size > 0 && file_length > 0)
		lp_update_media_info(lent->mid, file_length, file_size);

	return ECM_TRUE;
}

lparser_op_t	lparser_op = {
	ms2wgen_parse,
};
